package view;


import domain.Admin;
import domain.Customer;
import proxy.CustomerProxy;
import service.CustomerService;

import java.util.Scanner;

public class Loading {
    private boolean flag;
    private Scanner input = new Scanner(System.in);
    //中间类 -- 客户端与服务器相连传递信息
    private CustomerService proxy= (CustomerService) CustomerProxy.getProxy(CustomerService.class, "127.0.0.1", 9999);


    //用户登录
    private void loginUser(){
        System.out.println("------用户登录界面------");
        Customer customer = new Customer();
        System.out.println("请输入账号:");
        customer.setName(input.next());
        System.out.println("请输入密码：");
        customer.setPassword(input.next());
        //将输进来的信息与数据库内的信息进行比较
        boolean b = proxy.loginUser(customer);
        System.out.println(b?"登陆成功":"登录失败");


    }

    // 用户注册
    private void register(){
        Customer customer= new Customer();
        System.out.println("------用户注册界面------");
        System.out.print("设置账号：");
        customer.setName(input.next());
        System.out.print("设置密码：");
        customer.setPassword(input.next());
        //需要判断两次密码是否一致
        System.out.print("确认密码：");
        customer.setPassword(input.next());
        System.out.print("请输入电话：");
        customer.setAccount(input.next());
        //需要提交给中间类，存储到数据库
        boolean b = proxy.registerUser(customer);
        System.out.println(b?"注册成功":"注册失败");

    }

    //管理员登录
    private void loginAdmin(){
        System.out.println("------管理员登录界面------");
        Admin admin = new Admin();
        System.out.print("请输入账号：");
        admin.setName(input.next());
        System.out.print("请输入密码：");
        admin.setPassword(input.next());
        //将输入的管理员的信息与数据库内的信息进行比较
        boolean b = proxy.loginAdmin(admin);
        System.out.println(b?"管理员登录成功":"管理员登录失败");

    }



    //选择登录界面
    public void menu() {
        flag = true;
        while (flag) {
            System.out.println("--------登录界面--------");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("3.管理员登录");
            System.out.println("4.退 出");
            System.out.println("请输入你的选项：");
            int a = input.nextInt();
            switch (a) {
                case 1:
                    loginUser();
                    break;
                case 2:
                    register();
                    break;
                case 3:
                    loginAdmin();
                    break;
                case 4:
                    System.out.println("输入y退出,输入0撤销");
                    String str = input.next();
                    if (str.equals("y") || str.equals("Y")) {
                        System.out.println("退出成功");
                        flag = false;
                        break;
                    }else {
                        System.out.println("退出失败");
                        continue;
                    }
            }
        }

    }



}
